//
//  XMLWelcomeViewController.swift
//  Weibo
//
//  Created by mac on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class XMLWelcomeViewController: UIViewController {
    
    private lazy var backImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var headImageView :UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        if let url = XMLUserAcountViewModel.sharedUserAcountViewModel.userAcount?.avatar_large {
        
            imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        return imageView

    }()
    
    private lazy var nameLable :UILabel = {
    
        let lable = UILabel()
        if let name = XMLUserAcountViewModel.sharedUserAcountViewModel.userAcount?.name {
        
            lable.text = name + " ,欢迎回来"
        } else {
        
            lable.text = "欢迎回来"
        }
        lable.font = UIFont.systemFontOfSize(15)
        lable.textColor = UIColor.darkGrayColor()
        return lable
    }()
    
    override func viewDidAppear(animated: Bool) {
        loadAnimation()
    }
    func loadAnimation() {
    
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        nameLable.alpha = 0
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.nameLable.alpha = 1
                    }, completion: { (_) -> Void in
                     NSNotificationCenter.defaultCenter().postNotificationName(updateRootVCNotification, object: self)
                })
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.orangeColor()
        addSubViews()
    }

    func addSubViews(){
    
        view.addSubview(backImageView)
        view.addSubview(headImageView)
        view.addSubview(nameLable)
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
        
        
    }
    


}
